package com.boodaba.todocarts.infstr;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;

public class Db4oContainerHolder {
	private ObjectContainer mContainer;
	private String mDbFilePath;
	
	public Db4oContainerHolder(String storeFilePath) {
		mDbFilePath = storeFilePath;
	}
	
	public ObjectContainer getContainer() {
		if (mContainer == null
			|| mContainer.ext().isClosed()) {
			EmbeddedConfiguration conf = Db4oEmbedded.newConfiguration();
			conf.common().updateDepth(3);
			mContainer = Db4oEmbedded.openFile(conf, mDbFilePath);
		}
		return mContainer;
	}
	
	public void closeContainer() {
		if (mContainer != null
			|| !mContainer.ext().isClosed()) {
			mContainer.close();
			mContainer = null;
		}
	}
	
	public String getDbPath() {
		return mDbFilePath;
	}
	
	@Override
	protected void finalize() throws Throwable {
		closeContainer();
		super.finalize();
	}
}
